#ifndef _COM_PUBLIC_H__
#define _COM_PUBLIC_H__
#include "base_types.h"

#ifndef MSB
#define MSB(x) (((x) >> 8) & 0xff)
#endif

#ifndef LSB
#define LSB(x) ((x) & 0xff)
#endif
#ifndef SHORTSWAP
#define SHORTSWAP(x) (MSB(x) | (LSB(x) << 8))
#endif

#ifndef LONGSWAP
#define LLSB(x) ((x) & 0xff)
#define LNLSB(x) (((x) >> 8) & 0xff)
#define LNMSB(x) (((x) >> 16) & 0xff)
#define LMSB(x) (((x) >> 24) & 0xff)
#define LONGSWAP(x) ((LLSB(x) << 24) |  \
                     (LNLSB(x) << 16) | \
                     (LNMSB(x) << 8) |  \
                     (LMSB(x)))
#endif

#ifndef IGNORE
#define IGNORE (0)
#endif
#define WARNING (1)
#define CRITICAL (2)
#define FATAL (3)
#define ERR_MAX (4)

extern s08 *l_ErrLevelDsc[ERR_MAX];
extern int g_iDbgLevel;
// s32 g_iDbgLevel = CRITICAL;
// s08 *l_ErrLevelDsc[ERR_MAX] = {"ignore", "warnning", "critcal", "fatal"};

#if (defined WIN32)
#define FILE_NAME(x) (strrchr(x, '/') + 1)
#define NFILE_NAME(x) (x ? (strrchr((char *)x, '\\') + 1) : "NULL")
#else
#define FILE_NAME(x) (strrchr(x, '/') + 1)
#define NFILE_NAME(x) (x ? (strrchr((char *)x, '/') + 1) : "NULL")
#endif

#define ERR_LEVEL(x) (l_ErrLevelDsc[x])

#define IF_ERROR_RETURN(x, level, file, line)                                                    \
    {                                                                                            \
        if ((x) == BASE_ERROR)                                                                   \
        {                                                                                        \
            if (level >= g_iDbgLevel)                                                            \
            {                                                                                    \
                printf("###Error occur, %s @%s @%d\n", ERR_LEVEL(level), FILE_NAME(file), line); \
            }                                                                                    \
            return BASE_ERROR;                                                                   \
        }                                                                                        \
    }

#define IF_ERROR_RETURN_VALUE(x, y, level, file, line)                                           \
    {                                                                                            \
        if ((x) == BASE_ERROR)                                                                   \
        {                                                                                        \
            if (level >= g_iDbgLevel)                                                            \
            {                                                                                    \
                printf("###Error occur, %s @%s @%d\n", ERR_LEVEL(level), FILE_NAME(file), line); \
            }                                                                                    \
            return y;                                                                            \
        }                                                                                        \
    }

#define IF_EQUAL_RETURN(x, y, level, file, line)                                                 \
    {                                                                                            \
        if (x == y)                                                                              \
        {                                                                                        \
            if (level >= g_iDbgLevel)                                                            \
            {                                                                                    \
                printf("###Error occur, %s @%s @%d\n", ERR_LEVEL(level), FILE_NAME(file), line); \
            }                                                                                    \
            return BASE_ERROR;                                                                   \
        }                                                                                        \
    }

#define IF_NOT_EQUAL_RETURN(x, y, level, file, line)                                             \
    {                                                                                            \
        if (x != y)                                                                              \
        {                                                                                        \
            if (level >= g_iDbgLevel)                                                            \
            {                                                                                    \
                printf("###Error occur, %s @%s @%d\n", ERR_LEVEL(level), FILE_NAME(file), line); \
            }                                                                                    \
            return BASE_ERROR;                                                                   \
        }                                                                                        \
    }

#define IF_GREAT_RETURN(x, y, level, file, line)                                                 \
    {                                                                                            \
        if (x > y)                                                                               \
        {                                                                                        \
            if (level >= g_iDbgLevel)                                                            \
            {                                                                                    \
                printf("###Error occur, %s @%s @%d\n", ERR_LEVEL(level), FILE_NAME(file), line); \
            }                                                                                    \
            return BASE_ERROR;                                                                   \
        }                                                                                        \
    }

#define IF_LESS_RETURN(x, y, level, file, line)                                                  \
    {                                                                                            \
        if (x < y)                                                                               \
        {                                                                                        \
            if (level >= g_iDbgLevel)                                                            \
            {                                                                                    \
                printf("###Error occur, %s @%s @%d\n", ERR_LEVEL(level), FILE_NAME(file), line); \
            }                                                                                    \
            return BASE_ERROR;                                                                   \
        }                                                                                        \
    }

#define IF_NOT_LESS_RETURN(x, y, level, file, line)                                              \
    {                                                                                            \
        if (x >= y)                                                                              \
        {                                                                                        \
            if (level >= g_iDbgLevel)                                                            \
            {                                                                                    \
                printf("###Error occur, %s @%s @%d\n", ERR_LEVEL(level), FILE_NAME(file), line); \
            }                                                                                    \
            return BASE_ERROR;                                                                   \
        }                                                                                        \
    }

#define IF_NOT_GREAT_RETURN(x, y, level, file, line)                                             \
    {                                                                                            \
        if (x <= y)                                                                              \
        {                                                                                        \
            if (level >= g_iDbgLevel)                                                            \
            {                                                                                    \
                printf("###Error occur, %s @%s @%d\n", ERR_LEVEL(level), FILE_NAME(file), line); \
            }                                                                                    \
            return BASE_ERROR;                                                                   \
        }                                                                                        \
    }

#define IF_NOT_IN_RANGE_RETURN(x, y, z, level, file, line)                                       \
    {                                                                                            \
        if (x < y || (x) > z)                                                                    \
        {                                                                                        \
            if (level >= g_iDbgLevel)                                                            \
            {                                                                                    \
                printf("###Error occur, %s @%s @%d\n", ERR_LEVEL(level), FILE_NAME(file), line); \
            }                                                                                    \
            return BASE_ERROR;                                                                   \
        }                                                                                        \
    }

#define IF_EQUAL_RETURN_VALUE(x, y, z, level, file, line)                                        \
    {                                                                                            \
        if (x == y)                                                                              \
        {                                                                                        \
            if (level >= g_iDbgLevel)                                                            \
            {                                                                                    \
                printf("###Error occur, %s @%s @%d\n", ERR_LEVEL(level), FILE_NAME(file), line); \
            }                                                                                    \
            return z;                                                                            \
        }                                                                                        \
    }

#define IF_NOT_EQUAL_RETURN_VALUE(x, y, z, level, file, line)                                    \
    {                                                                                            \
        if (x != y)                                                                              \
        {                                                                                        \
            if (level >= g_iDbgLevel)                                                            \
            {                                                                                    \
                printf("###Error occur, %s @%s @%d\n", ERR_LEVEL(level), FILE_NAME(file), line); \
            }                                                                                    \
            return z;                                                                            \
        }                                                                                        \
    }

#define IF_GREAT_RETURN_VALUE(x, y, z, level, file, line)                                        \
    {                                                                                            \
        if (x > y)                                                                               \
        {                                                                                        \
            if (level >= g_iDbgLevel)                                                            \
            {                                                                                    \
                printf("###Error occur, %s @%s @%d\n", ERR_LEVEL(level), FILE_NAME(file), line); \
            }                                                                                    \
            return z;                                                                            \
        }                                                                                        \
    }

#define IF_LESS_RETURN_VALUE(x, y, z, level, file, line)                                         \
    {                                                                                            \
        if (x < y)                                                                               \
        {                                                                                        \
            if (level >= g_iDbgLevel)                                                            \
            {                                                                                    \
                printf("###Error occur, %s @%s @%d\n", ERR_LEVEL(level), FILE_NAME(file), line); \
            }                                                                                    \
            return z;                                                                            \
        }                                                                                        \
    }

#define IF_NOT_LESS_RETURN_VALUE(x, y, z, level, file, line)                                     \
    {                                                                                            \
        if (x >= y)                                                                              \
        {                                                                                        \
            if (level >= g_iDbgLevel)                                                            \
            {                                                                                    \
                printf("###Error occur, %s @%s @%d\n", ERR_LEVEL(level), FILE_NAME(file), line); \
            }                                                                                    \
            return z;                                                                            \
        }                                                                                        \
    }

#define IF_NOT_GREAT_RETURN_VALUE(x, y, z, level, file, line)                                    \
    {                                                                                            \
        if (x <= y)                                                                              \
        {                                                                                        \
            if (level >= g_iDbgLevel)                                                            \
            {                                                                                    \
                printf("###Error occur, %s @%s @%d\n", ERR_LEVEL(level), FILE_NAME(file), line); \
            }                                                                                    \
            return z;                                                                            \
        }                                                                                        \
    }

#define IF_NOT_IN_RANGE_RETURN_VALUE(x, y, z, r, level, file, line)                                        \
    {                                                                                                      \
        if (x < y || x > z)                                                                                \
        {                                                                                                  \
            if (level >= g_iDbgLevel)                                                                      \
            {                                                                                              \
                printf("###Error occur out range, %s @%s @%d\n", ERR_LEVEL(level), FILE_NAME(file), line); \
            }                                                                                              \
            return r;                                                                                      \
        }                                                                                                  \
    }

#define IF_NOT_EQUAL_DO(x, y, z) \
    {                            \
        if (x != (y))            \
            z;                   \
    }
#define IF_EQUAL_DO(x, y, z) \
    {                        \
        if (x == y)          \
            z;               \
    }
#define IF_NOT_IN_RANGE_RETURN_DO(x, y, z, r) \
    {                                         \
        if (x < y || x > z)                   \
            r;                                \
    }

#define IF_LESS_INC(x, y) \
    {                     \
        if (x < y)        \
            x++;          \
    }
#define IF_GREAT_DEC(x, y) \
    {                      \
        if (x > y)         \
            x--;           \
    }
#define FUN_PARM_ERROR_HINT(level, file, line)                                                  \
    {                                                                                           \
        if (level >= g_iDbgLevel)                                                               \
        {                                                                                       \
            printf("###Fun parm error, %s @%s @%d\n", ERR_LEVEL(level), FILE_NAME(file), line); \
        }                                                                                       \
        return BASE_ERROR;                                                                      \
    }

#ifdef __cplusplus
extern "C"
{
#endif

#ifdef __cplusplus
}
#endif
#endif
